﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Omnia.DataModels;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Data;
using Omnia.Library;

namespace Omnia.ViewModels
{
    public class TransactionViewModel:ViewModelBase
    {
        private DateTime selectedDate = DateTime.Now;
        public DateTime SelectedDate
        {
            get { return selectedDate; }
            set
            {
                selectedDate = value;
                RaisePropertyChanged(() => SelectedDate);
                RaisePropertyChanged(() => IncomeCollection);
                RaisePropertyChanged(() => ExpenseCollection);
            }
        }

        public ObservableCollection<TransactionGroupInfo> IncomeCollection
        {
            get
            {
                IEnumerable<TransactionInfo> transactions = Database.CollectionOfTransactions.Where(t => t.Kind == Enums.TransactionKindValue.Income);// && t.Year == SelectedDate.Year && t.Month == SelectedDate.Month);
                IEnumerable<TransactionInfo> _t = transactions.Where(t => t.Year == SelectedDate.Year && t.Month == SelectedDate.Month);//.Select(t=>new TransactionInfo(;
                foreach (TransactionInfo t in _t)
                {
                    t.Category = Database.CollectionOfCategories.FirstOrDefault(c => c.Guid == t.CategoryGuid);
                    if (!string.IsNullOrWhiteSpace(t.CaseGuid))
                    {
                        t.Case = Database.CollectionOfCases.FirstOrDefault(c => c.Guid == t.CaseGuid);
                    }
                }
                return new ObservableCollection<TransactionGroupInfo>(GetGroup(_t));
            }
        }

        public ObservableCollection<TransactionGroupInfo> ExpenseCollection
        {
            get
            {
                IEnumerable<TransactionInfo> transactions = Database.CollectionOfTransactions.Where(t => t.Kind == Enums.TransactionKindValue.Expense);// && t.Year == SelectedDate.Year && t.Month == SelectedDate.Month);
                IEnumerable<TransactionInfo> _t = transactions.Where(t => t.Year == SelectedDate.Year && t.Month == SelectedDate.Month);//.Select(t=>new TransactionInfo(;
                foreach (TransactionInfo t in _t)
                {
                    t.Category = Database.CollectionOfCategories.FirstOrDefault(c => c.Guid == t.CategoryGuid);
                    if (!string.IsNullOrWhiteSpace(t.CaseGuid))
                    {
                        t.Case = Database.CollectionOfCases.FirstOrDefault(c => c.Guid == t.CaseGuid);
                    }
                }
                return new ObservableCollection<TransactionGroupInfo>(GetGroup(_t));
            }
        }

        private IEnumerable<TransactionGroupInfo> GetGroup(IEnumerable<TransactionInfo> transactions)
        {
            int[] days = transactions.OrderByDescending(t => t.Time).Select(t => t.Day).Distinct().ToArray();
            if (days.Count() > 0)
            {
                for (var i = 0; i < days.Count(); i++)
                {
                    int day = days[i];
                    yield return new TransactionGroupInfo(transactions.Where(t => t.Day == day), SelectedDate.Year, SelectedDate.Month, day);
                }
            }
        }

        public TransactionInfo TransactionItemToRemove
        {
            get
            {
                if (this.TransactionGuid != null)
                {
                    return Database.CollectionOfTransactions.FirstOrDefault(t => t.Guid == TransactionGuid);
                }
                return null;
            }
        }

        private string transactionGuid;
        public string TransactionGuid
        {
            get { return transactionGuid; }
            set
            {
                transactionGuid = value;
                RaisePropertyChanged(() => TransactionGuid);
                RaisePropertyChanged(() => TransactionItemToRemove);
            }
        }

        #region ICommand
        public ICommand IncreaseMonthCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        SelectedDate = SelectedDate.AddMonths(1); 
                        RaisePropertyChanged(() => SelectedDate);
                    }
                };
            }
        }

        public ICommand DecreaseMonthCommand
        {
            get
            {
                return new GenericCommand() { ExecuteCallback = delegate { SelectedDate = SelectedDate.AddMonths(-1); RaisePropertyChanged(() => SelectedDate); } };
            }
        }

        public ICommand RemoveTransactionCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        Database.CollectionOfTransactions.DeleteOnSubmit(TransactionItemToRemove); Save();
                        RaisePropertyChanged(() => IncomeCollection);
                        RaisePropertyChanged(() => ExpenseCollection);
                        TransactionGuid = null;
                    }
                };
            }
        }
        #endregion
    }
}
